<template>
  <el-dialog
    :title="dataForm.id?'修改角色':'新增角色'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
    <el-form-item label="角色名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="请输入角色名称"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        isdisabled: false,
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          deleted: ''
        },
        dataRule: {
          name: [
            { required: true, message: '角色名称不能为空！', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '角色描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (info) {
        if(info){
          this.dataForm.id =  info.roleId
          this.dataForm.name= info.roleName
        }else{
          this.dataForm.id = 0
        }
        this.visible = true
  
        // this.$nextTick(() => {
        //   if (this.dataForm.id) {
        //     this.$http({
        //       url: this.$http.adornUrl(
        //       `/admin/role/menu/list?roleId=${this.dataForm.id}`
        //     ),
        //       method: 'get',
        //       params: this.$http.adornParams()
        //     }).then(({ data }) => {
        //       if (data && data.code === '200') {
        //         this.dataForm.name = data.data.roleName
        //         this.dataForm.deleted = data.data.introduction
        //       }
        //     })
        //   }
        // })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          let roleId = {
          }
          if (this.dataForm.id) {
            roleId.roleId = this.dataForm.id
          }
          if (valid) {
            this.isdisabled = true
            this.$http({
              url: this.$http.adornUrl(this.dataForm.id ? '/admin/role/update' : `/admin/role/add`),
              method: 'post',
              data: this.$http.adornData({
	                'roleName': this.dataForm.name,
                ...roleId
              })
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
              this.isdisabled = false
            })
          }
        })
      }
    }
  }
</script>
